package com.jye.rseven.user.controller;



import com.jye.rseven.domain.TestUser;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.UserService;
import com.jye.rseven.domain.JsonResult;
import com.jye.rseven.user.util.JwtUtils;
import com.jye.rseven.user.util.RedisUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/user")
@Api(tags="用户控制器API")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RedisUtils redisUtils;


    @Value("${myconstant.WX_APP_ID}")
    private String WX_APP_ID;// appId

    @Value("${myconstant.WX_APP_KEY}")
    private String WX_APP_KEY;// AppSecret

    @GetMapping("/test")
    public JsonResult test(HttpServletRequest request){
        return new JsonResult("0","测试成功!","");
//        return "WX_APP_ID:"+WX_APP_ID+"\t "+ "WX_APP_KEY: "+WX_APP_KEY;
    }

    @PostMapping("/testLogin")
    public JsonResult testLogin(User user){
        User t_user = userService.testLogin(user);
        if(!StringUtils.isEmpty(t_user)){
            //正确
            String token = JwtUtils.geneJsonWebToken(t_user);

            return new JsonResult<>("200","登录成功!",token);
        }else{
            //错误
            return new JsonResult<>("404","用户名或者密码错误!","");
        }
    }

    /*
    * 通过id查找用户
    * */
    @GetMapping("/findUserById")
    @ApiOperation(value="查找用户", notes="根据用户id查找用户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findUserById(int id){
        JsonResult jsonResult = new JsonResult();
        User u = userService.findUserById(id);
        if (!StringUtils.isEmpty(u)){
            jsonResult.setData(u);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
    * 提交code获取session_key和openid
    * */
    @GetMapping("/commitCode")
    public String commitCode(String code){
        return userService.commiteCode(code,WX_APP_ID,WX_APP_KEY);
    }

    /*
    * 通过openid查找用户,若无则添加用户
    * */
    @GetMapping("/findUserByOpenId")
    public JsonResult findUserByOpenId(String openId){
        JsonResult jsonResult = new JsonResult();
        User user = userService.findUserByOpenId(openId);
        if(!StringUtils.isEmpty(user)){
            //登录成功
            //加登录记录
            userService.userLoginById(user.getId());
            jsonResult.setMsg("登录成功!");
            jsonResult.setData(user);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("登录失败!");
            jsonResult.setData(null);
        }
        return jsonResult;
    }

    @PutMapping("/updateUser")
    @ApiOperation(value="修改用户头像和昵称", notes="根据id,头像avatarurl,昵称nickname修改用户", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "user",
                    value = "用户", required = true),
    })
    public JsonResult updateUser(User user){
        JsonResult jsonResult = new JsonResult();
        System.out.println(user);
        //避免修改其它
        User t_user = new User();
        //User user = new User();
        t_user.setNickname(user.getNickname());
        t_user.setAvatarurl(user.getAvatarurl());
        t_user.setId(user.getId());
        int i = userService.updateUser(t_user);
        if(i!=0){
          jsonResult.setMsg("修改成功!");
        }else{
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @GetMapping("/findUserLoginHistoryById")
    @ApiOperation(value="查找用户登录历史记录", notes="根据id查找记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "用户id", required = true),
    })
    public JsonResult findUserLoginHistoryById(int id){
        JsonResult jsonResult = new JsonResult();
        List<Date> dateList = userService.findUserLoginHistoryById(id);
        if(dateList.size()!=0){
            jsonResult.setData(dateList);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!暂无您登录记录!如有异常请联系客服!");
        }
        return jsonResult;
    }



}
